<?php

class wms_mdl_deliverynote extends dbeav_model{
    
    
   
    public function searchOptions(){
        $parentOptions = parent::searchOptions();
        $childOptions = array(
            'delivery_bn'=>app::get('base')->_('发货单号'),
            'order_bn'=>app::get('base')->_('订单号'),
            'axshipno'=>app::get('base')->_('AX送货单号'),
            'axorderno'=>app::get('base')->_('AX订单号'),
        );
        return $Options = array_merge($parentOptions,$childOptions);
    }

    public function _filter($filter,$tableAlias=null,$baseWhere=null)
    {   
        if (isset($filter['order_bn']))
        {
            $order_bn = $filter['order_bn'];
            $sql = "SELECT distinct dli.deliverynote_id from  sdb_wms_delivery e 
         inner JOIN sdb_ome_delivery od on ( e.outer_delivery_bn = od.delivery_bn )
         inner JOIN sdb_ome_delivery_order odo on ( od.delivery_id = odo.delivery_id )
         inner JOIN sdb_ome_orders os on (odo.order_id = os.order_id)
        inner join sdb_wms_deliverynote_line dli on dli.delivery_bn=e.delivery_bn
            where os.order_bn='$order_bn'";
            
            $_rows = $this->db->select($sql);
            if(!empty($_rows)){
                foreach($_rows as $_orders){
                    $deliverynote_id[] = $_orders['deliverynote_id'];
                }
                $where .= ' AND id IN ('.implode(',', $deliverynote_id).')';
                unset($filter['order_bn']);
            }
        }
        #发货单
        if(isset($filter['delivery_bn'])){
            $delivery_bn = $filter['delivery_bn'];
            $sql = "select deliverynote_id from sdb_wms_deliverynote_line  where  delivery_bn= '$delivery_bn'";
            $_rows = $this->db->select($sql);
            if(!empty($_rows))
            {
                foreach($_rows as $_orders){
                    $deliverynote_id[] = $_orders['deliverynote_id'];
                }
                $where .= ' AND id IN ('.implode(',', $deliverynote_id).')';
                unset($filter['delivery_bn']);
            }

        }
        #AX送货单号
        if(isset($filter['axshipno'])){
            $axshipno = $filter['axshipno'];
            $sql = "select deliverynote_id from sdb_wms_deliverynote_line  where  axshipno= '$axshipno'";
            $_rows = $this->db->select($sql);
            if(!empty($_rows))
            {
                foreach($_rows as $_orders){
                    $deliverynote_id[] = $_orders['deliverynote_id'];
                }
                $where .= ' AND id IN ('.implode(',', $deliverynote_id).')';
                unset($filter['axshipno']);
            }

        }
        #AX订单号
        if(isset($filter['axorderno'])){
            $axorderno = $filter['axorderno'];
            $sql = "select deliverynote_id from sdb_wms_deliverynote_line  where  axorderno= '$axorderno'";
            $_rows = $this->db->select($sql);
            if(!empty($_rows))
            {
                foreach($_rows as $_orders){
                    $deliverynote_id[] = $_orders['deliverynote_id'];
                }
                $where .= ' AND id IN ('.implode(',', $deliverynote_id).')';
                unset($filter['axorderno']);
            }

        }

        return parent::_filter($filter,$tableAlias,$baseWhere).$where;
    }
    function getDelivery($order_id) {
        
        $sql = "SELECT distinct dn.deliverynote_bn,dn.create_time,sp.AXCustomer,sp.contact,sp.contacttel,sp.shipaddress,sp.operator,sp.soprojecter,sp.customername,sp.accountmethod,sp.shortname,
dnl.carno, axshipno , axorderno
		FROM  sdb_wms_deliverynote dn 
		LEFT JOIN sdb_wms_deliverynote_line dnl on ( dn.id = dnl.deliverynote_id )
		LEFT JOIN sdb_wms_delivery d on ( dnl.delivery_id = d.delivery_id)
		left join sdb_wms_delivery_items di on d.delivery_id=di.delivery_id
		left join sdb_ome_shop sp on dnl.shop_id=sp.shop_id
		LEFT JOIN sdb_material_basic_material AS P ON di.item_id=P.bm_id
		LEFT JOIN sdb_material_basic_material_ext AS pext ON P.bm_id=pext.bm_id
                where dn.id = '$order_id' ";
        $rows = $this->db->select($sql);
        return $rows;
        
    }    

    function getDeliveryInfo($order_id,$AXCustomer) {
        
        $sql = "SELECT sp.AXCustomer,p.material_name,p.material_bn,pcode.code, pext.unit,pext.specifications, sum(di.number) number
		FROM  sdb_wms_deliverynote dn 
		LEFT JOIN sdb_wms_deliverynote_line dnl on ( dn.id = dnl.deliverynote_id )
		LEFT JOIN sdb_wms_delivery d on ( dnl.delivery_id = d.delivery_id)
		left join sdb_wms_delivery_items di on d.delivery_id=di.delivery_id
		left join sdb_ome_shop sp on dnl.shop_id=sp.shop_id
		LEFT JOIN sdb_material_basic_material AS P ON di.product_id=P.bm_id
		LEFT JOIN sdb_material_basic_material_ext AS pext ON P.bm_id=pext.bm_id
		left join sdb_material_codebase pcode on p.bm_id=pcode.bm_id
                where dn.id = '$order_id' and sp.AXCustomer='$AXCustomer'
	group by sp.AXCustomer,p.material_name,p.material_bn,pcode.code, pext.unit,pext.specifications ";
        $rows = $this->db->select($sql);
        return $rows;
    } 
}
